<template>
  <div>
    <search placeholder="请输入学生的名字" @result-click="resultClick" @on-change="getResult" :results="results" v-model="value" position="absolute" auto-scroll-to-top top="0px" @on-focus="onFocus" @on-cancel="onCancel"></search>
  </div>
</template>
<script>
  import { Search, Group, Cell } from 'vux'

  export default {
    components: {
      Search,
      Group,
      Cell
    },
    created: function () {
    },
    methods: {
      resultClick (item) {
        this.$router.push({path: 'StudentExamList', query: {studentid: item.id}})
      },
      getResult (val) {
        if (val.match(/[^\u4e00-\u9fa5]/g) || val === '') {
          return
        }
        this.$http.get('/jeewx/studentController.do?searchStudent&studentName=' + val)
          .then(function (res) {
            console.log(res.body.jsonStr)
            this.results = getResult(res.body.jsonStr)
          }, function (res) {
            console.log(res)
          })
      },
      onFocus () {
        console.log('on focus')
      },
      onCancel () {
        console.log('on cancel')
      }
    },
    data () {
      return {
        results: [],
        value: ''
      }
    }
  }

  function getResult (val) {
    let jsonArray = JSON.parse(val).obj
    let rs = []
    for (let i = 0; i < jsonArray.length; i++) {
      rs.push({
        title: jsonArray[i].chname,
        id: jsonArray[i].id
      })
    }
    return rs
  }
</script>

<style scoped>


</style>
